package com.lj.wordmark.utils;

import cn.hutool.core.collection.CollUtil;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.util.List;
import java.util.function.Function;

/**
 * @Author luojing
 * @Date 2022/4/13
 */
public class WordMarkUtils {

    /**
     * 复制文本run，包括复制样式
     *
     * @param source 来源run
     * @param target 目标run
     */
    public static void copyRun(XWPFRun source, XWPFRun target) {
        copyRun(source, target, source.text());
    }

    /**
     * 复制文本run，包括复制样式
     *
     * @param source 来源run
     * @param target 目标run
     * @param text   目标run的文本
     */
    public static void copyRun(XWPFRun source, XWPFRun target, String text) {
        copyRun(source, target, sourceText -> text);
    }

    public static void copyRun(XWPFRun source, XWPFRun target, Function<String, String> runTextFun) {
        if (source == null || target == null) {
            throw new RuntimeException("复制run时source和target都不得为空！");
        }
        target.getCTR().set(source.getCTR().copy());
        //设置文本
        target.setText(runTextFun.apply(source.text()), 0);
    }

    public static void copyParagraph(XWPFParagraph source, XWPFParagraph target) {
        copyParagraph(source, target, sourceRunText -> sourceRunText);
    }

    /**
     * 复制段落包括样式和 文本内容
     *
     * @param source 来段落
     * @param target 模板段落
     */
    public static void copyParagraph(XWPFParagraph source, XWPFParagraph target, Function<String, String> runTextFun) {
        if (source == null || target == null) {
            throw new RuntimeException("复制Paragraph时source和target都不得为空！");
        }
        // 文本
        if (runTextFun != null) {
            List<XWPFRun> sourceRuns = source.getRuns();
            if (CollUtil.isNotEmpty(sourceRuns)) {
                for (XWPFRun sourceRun : sourceRuns) {
                    XWPFRun targetRun = target.createRun();
                    copyRun(sourceRun, targetRun, runTextFun);
                }
            }
        }
        // 复制段落信息
        if (source.getCTP() != null && source.getCTP().getPPr() != null) {
            target.getCTP().setPPr(source.getCTP().getPPr());
        }
    }

    /**
     * 复制单元格
     *
     * @param source 来源cell
     * @param target 模板cell
     */
    public static void copyCell(XWPFTableCell source, XWPFTableCell target) {
        if (source == null || target == null) {
            throw new RuntimeException("复制Cell时source和target都不得为空！");
        }

        CTTc sourceCttc = source.getCTTc();
        CTTcPr sourceTcPr = sourceCttc.getTcPr();

        CTTc targetCttc = target.getCTTc();
        CTTcPr targetTcPr = targetCttc.addNewTcPr();

        //复制单元格宽度
        if (sourceTcPr.getTcW() != null) {
            targetTcPr.addNewTcW().setW(sourceTcPr.getTcW().getW());
        }
        //复制单元格对齐方式
        if (sourceTcPr.getVAlign() != null) {
            targetTcPr.addNewVAlign().setVal(sourceTcPr.getVAlign().getVal());
        }
        if (sourceCttc.getPList().size() > 0) {
            CTP ctp = sourceCttc.getPList().get(0);
            if (ctp.getPPr() != null && ctp.getPPr().getJc() != null) {
                targetCttc.getPList().get(0).addNewPPr().addNewJc().setVal(ctp.getPPr().getJc().getVal());
            }
        }
        //复制边框
        if (sourceTcPr.getTcBorders() != null) {
            targetTcPr.setTcBorders(sourceTcPr.getTcBorders());
        }
        //复制段落
        List<XWPFParagraph> sourceParagraphs = source.getParagraphs();
        if (!sourceParagraphs.isEmpty()) {
            List<XWPFParagraph> targetParagraphs = target.getParagraphs();
            int targetsize = targetParagraphs.size();
            for (int i = 0; i < sourceParagraphs.size(); i++) {
                XWPFParagraph targetParagraph;
                if (i < targetsize) {
                    targetParagraph = targetParagraphs.get(i);
                } else {
                    targetParagraph = target.addParagraph();
                }
                copyParagraph(sourceParagraphs.get(i), targetParagraph);
            }
        }
    }

    /**
     * 从fromIndex开始获取分隔符的位置，
     * 如果不存在返回-1
     * 如果存在返回索引
     */
    public static int getSeparatorIndexOf(String name, String delimiter, int fromIndex) {
        return name.indexOf(delimiter, fromIndex);
    }
}
